package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * cskaoyan
 */
@Slf4j
@Component
@Service
@Transactional
@SuppressWarnings("all")
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Resource
    OrderConverter orderConverter;

    @Override
    public OrderListResponse queryAllOrder(OrderListRequest request) {
        OrderListResponse orderListResponse = new OrderListResponse();
        try {
            request.requestCheck();
        } catch (Exception e) {
            log.error("OrderCoreServiceImpl.createOrder Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(orderListResponse, e);
            return orderListResponse;
        }
        PageHelper.startPage(request.getPage(), request.getSize());
        Example example = new Example(Order.class);
        example.setOrderByClause("updateTime");
        if (request.getSize() != null)
            example.setOrderByClause(request.getSort());
        Example.Criteria criteria = example.createCriteria();
        //这里不知道要给表的列名还是类的变量名
        criteria.andEqualTo("userId", request.getUserId());
        List<Order> orderList = orderMapper.selectByExample(example);
        PageInfo orderspageInfo = new PageInfo(orderList);
        List<Order> orders = orderspageInfo.getList();
        //假如没有订单直接给空
        if (orders.size() == 0) {
            OrderListResponse listResponse = new OrderListResponse();
            listResponse.setTotal((long) 0);
            listResponse.setData(new LinkedList<>());
            listResponse.setCode(OrderRetCode.SUCCESS.getCode());
            listResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
            return listResponse;
        }
        LinkedList<OrderDetailInfo> detailInfoLinkedList = new LinkedList<>();
        for (Order order : orders) {
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);
            detailInfoLinkedList.add(orderDetailInfo);

            //给地址
            //Example shippingExample = new Example(OrderShipping.class);
            //Example.Criteria criteria2 = shippingExample.createCriteria();
            //criteria2.andEqualTo("orderId", order.getOrderId());
            List<OrderShipping> orderShippings;
            try {
                OrderShipping orderShipping1 = orderShippingMapper.selectByPrimaryKey(order.getOrderId());

                OrderShipping orderShipping = orderShipping1;
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);

                //给订单商品
                Example example1 = new Example(OrderItem.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("orderId", order.getOrderId());
                List<OrderItem> orderItems;
                orderItems = orderItemMapper.selectByExample(example1);
                List<OrderItemDto> orderItemDtoList = orderConverter.item2dto(orderItems);
                orderDetailInfo.setOrderItemDto(orderItemDtoList);

            } catch (Exception e) {
                e.printStackTrace();
                orderListResponse.setCode(OrderRetCode.DB_EXCEPTION.getCode());
                orderListResponse.setMsg(OrderRetCode.DB_EXCEPTION.getMessage());
                return orderListResponse;
            }
        }
        //给总数

        orderListResponse.setTotal((long) orderspageInfo.getTotal());
        orderListResponse.setData(detailInfoLinkedList);
        orderListResponse.setCode(OrderRetCode.SUCCESS.getCode());
        orderListResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        return orderListResponse;

    }

    @Override
    public OrderDetailInfoResponse queryOrderDetail(OrderDetailRequest request) {
        OrderDetailInfoResponse orderDetailInfoResponse = new OrderDetailInfoResponse();
        try {
            request.requestCheck();
        } catch (Exception e) {
            log.error("OrderCoreServiceImpl.createOrder Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(orderDetailInfoResponse, e);
            return orderDetailInfoResponse;
        }

        String orderId = request.getOrderId();
        Order order;
        List<OrderItem> orderItemList;
        List<OrderShipping> orderShippingList;

        try {
            order = orderMapper.selectByPrimaryKey(orderId);
            if (order == null) {
                return new OrderDetailInfoResponse();
            }
            Example example = new Example(OrderItem.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("orderId", orderId);
            orderItemList = orderItemMapper.selectByExample(example);

            Example example1 = new Example(OrderShipping.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("orderId", orderId);
            orderShippingList = orderShippingMapper.selectByExample(example1);

            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShippingList.get(0));
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItemList);
            orderDetailInfoResponse.setGoodsList(orderItemDtos);
            orderDetailInfoResponse.setOrderTotal(order.getPayment().intValue());
            orderDetailInfoResponse.setStreetName(orderShippingDto.getReceiverAddress());
            orderDetailInfoResponse.setUserName(orderShippingDto.getReceiverName());
            orderDetailInfoResponse.setTel(orderShippingDto.getReceiverMobile());
            orderDetailInfoResponse.setUserId(order.getUserId());
            orderDetailInfoResponse.setOrderStatus(order.getStatus());
            orderDetailInfoResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderDetailInfoResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            orderDetailInfoResponse.setMsg(OrderRetCode.DB_EXCEPTION.getMessage());
            orderDetailInfoResponse.setCode(OrderRetCode.DB_EXCEPTION.getCode());
        }
        return orderDetailInfoResponse;
    }
}
